package les.projeto;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

public class AddSemestersView extends Activity {

	public Calendar initialDate, endDate;
	public int today;
	
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.addsemester);
	    initialDate = Calendar.getInstance();
	    onNewIntent(getIntent());	    
	
	    getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        
	    final DatePicker picker = (DatePicker)findViewById(R.id.datePicker2);
	    final DatePicker picker2 = (DatePicker)findViewById(R.id.DatePicker01);
	    
	    EditText name = (EditText)findViewById(R.id.editTextName);
	    name.setText("", TextView.BufferType.EDITABLE);
	    
	    picker.setCalendarViewShown(false);
	    picker2.setCalendarViewShown(false);
	    today = picker.getDayOfMonth();
	    
	    picker.init(initialDate.get(Calendar.YEAR), initialDate.get(Calendar.MONTH), today, null);
	    picker2.init(initialDate.get(Calendar.YEAR), initialDate.get(Calendar.MONTH), today, null);
	   
	    Button save = (Button)findViewById(R.id.buttonSave);
	    save.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				EditText name = (EditText)findViewById(R.id.editTextName);
				String nameText = name.getText().toString();
				if (nameText.isEmpty() || nameText.trim().equals("Enter Here")){
					AlertDialog.Builder builder = new AlertDialog.Builder(AddSemestersView.this);
					builder.setMessage("Please, enter a name")
					       .setCancelable(false)
					     
					       .setNeutralButton("Ok", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					                dialog.cancel();
					           }
					       });
					builder.show();
				}else {
					
					int day = picker.getDayOfMonth();
					int month = picker.getMonth();
					int year = picker.getYear();
					
					String datePicker = day + "-" + month + "-" + year;
					String datePicker2 = picker2.getDayOfMonth() + "-" + picker2.getMonth() + "-" + picker2.getYear();
								
					insertBD(nameText, datePicker, datePicker2);
					
					AlertDialog.Builder builder = new AlertDialog.Builder(AddSemestersView.this);
					builder.setMessage("Semester Save")
					       .setCancelable(false)
					     
					       .setNeutralButton("Ok", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					                dialog.cancel();
					                back();
					           }
					       });
					builder.show();
					
//					
				}
				
			}
		});
	    
	}
	
	protected void insertBD(String name, String initialDate, String endDate) {
		BD bd = new BD();
		bd.insertSemester(this, name, initialDate, endDate);		
	}

	public void onNewIntent(Intent intent) {
		String dateText = intent.getStringExtra("date");
		String[] dateArr = dateText.split("-"); // date format is yyyy-mm-dd

		initialDate.set(Integer.parseInt(dateArr[0]), Integer.parseInt(dateArr[1]), Integer.parseInt(dateArr[2]));
	}
	
	@Override
	public void onBackPressed() {
		
        back();	        		 
        return;
	}

	private void back() {
		int month = initialDate.get(Calendar.MONTH);
        int year = initialDate.get(Calendar.YEAR);
        
	    Intent trocatela = new Intent(AddSemestersView.this, SemestersView.class);
		trocatela.putExtra("date", year+"-"+month+"-"+today);

		AddSemestersView.this.startActivity(trocatela);
		AddSemestersView.this.finish();
	}
}
